        var express = require("express");
var app = express();
const cors = require('cors');
var net = require('net');
var clicent = new net.Socket();
var path = require('path');
var fs = require('fs');
var mutipart = require('connect-multiparty');
var mutipartMiddeware = mutipart();
var winston = require('winston');
let { redisClient, getItem, setItem } = require('./redis');
const axios = require('axios')


// app.use(mutipart({ uploadDir: 'D:/share/web' }));
app.use(mutipart({ uploadDir: 'Z:/web' }));
var overageBuffer = null;  //之前的Buffer 剩余数据
var put = null; 


//   封包方法
function packet(buffer, msg) {
    let head = 'A5A5A5A5A5';
    let ip = clicent.localAddress;
    let port = clicent.localPort;
    let type = msg;
    Streamid = pathnumber();
    console.log('封包内部消息id', Streamid)
    let content = JSON.stringify(buffer);
    let contentLen = getUtf8StrLen(content);
    let tail = '5A5A5A5A5A';

    let buf = Buffer.alloc(20024, 0);             ///     定义buffer实例大小 
    buf.fill(head, 0, 0 + 10);                      ///     定义包头的字节大小   10字节
    buf.fill(ip, 10, 10 + ip.length);               ///     定义IP长度          10字节起，IP的实际长度 
    buf.writeUInt32LE(port, 25);                 ///     定义端口号长度       4个字节     
    buf.writeUInt32LE(type, 29);                 ///     定义type是实物id 也就是每一个页面传参的id
    buf.fill(Streamid, 33, 33 + 20);                      ///     定义消息id           20个字节 
    buf.writeUInt32LE(contentLen, 53);           ///     消息体长度           4个字节
    buf.fill(content, 57, 57 + contentLen);         ///     定义包体长度         字节长度按照实际大小
    buf.fill(tail, 57 + contentLen, 57 + contentLen + 10);       /// 定义包尾        字节长
    let dataLen = 10 + 15 + 4 + 4 + 20 + 4 + contentLen + 10;
    let dateLenth = buf.slice(0, dataLen);
    // console.log('1111111111',dateLenth)
    return dateLenth;
}
// 随机生成20位消息id方法
function pathnumber() {
    var data = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f",];
    for (var j = 0; j < 500; j++) {
        var result = "";
        for (var i = 0; i < 20; i++) {
            r = Math.floor(Math.random() * 16);
            result += data[r];
        }
    }
    return result
}

// UTF8字符集实际长度计算 
function getUtf8StrLen(str) {
    var realLength = 0;
    var len = str.length;
    var charCode = -1;
    for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128) {
            realLength += 1;
        } else {
            // 如果是中文则长度加3 
            realLength += 3;
        }
    }
    return realLength;
}

//   获取整包方法
function getPackageLength(buffer) {
    if (buffer.length < 10) {
        return 0;
    }
    return 10 + buffer.readUInt32LE(63);
}

///解包
function subpackage(buffer) {
    //  var buffer = Buffer.from(data);
    const heard = buffer.slice(0, 0 + 10);                // 截取包头
    console.log('拆包后获取到的包头', heard.toString())
    const ip = buffer.slice(10, 10 + 14);                 // 截取IP
    console.log('拆包后获取到的IP', ip.toString())
    const prot = buffer.readUInt32LE(25);                 // 截取端口号
    console.log('拆包后获取到的端口号', prot)
    const date = buffer.slice(33, 33 + 20);               // 截取时间段
    console.log('拆包后获取的时间', date.toString())
    const typer = buffer.readUInt32LE(29);                // 截取页面编码id   
    console.log('拆包后获取到的页面编码id', typer);
    const contenLenr = buffer.readUInt32LE(53);           // 截取消息体长度
    // console.log('消息体长度', contenLenr);
    const contentr = buffer.slice(57, 57 + contenLenr);        //截取数据内容
    // console.log('拆包获取的包体数据', contentr.toString());
    const tail = buffer.slice(57 + contenLenr, 57 + contenLenr + 10);    //截取包尾
    console.log('拆包后得到的包尾', tail.toString())
    return {
        heard: heard.toString(),
        ip: ip.toString(),
        prot,
        date: date.toString(),
        typer,
        contenLenr,
        contentr: contentr.toString(),
        tail: tail.toString()
    }
}



let coent = '<affairsid>1</affairsid>';
let affcoent = '<content><affaircount>30</affaircount><cpu>20</cpu><sata>15</sata><mem>48</mem><workstatus>1</workstatus><dbstatus>1</dbstatus><redisstatus>0</redisstatus><ftpstatus>0</ftpstatus></content>'
function timer() {             ////心跳包
    setInterval(() => {
        clicent.write(packet(coent, 1));
        clicent.write(packet(affcoent, 3));
    }, 6000);
}


var options = {
    host:'10.1.109.237',
    // host: '10.1.130.79',
    // host: '10.1.131.186',
    port: 7183
}
app.use(express.static('Z:/pic'))
// app.use(express.static('Z:/imageToWeb'));
app.use(express.static('Z:/'));


    //  设置亲求响应头
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
// app.use(express.static('public'))



var hostnamer = "10.1.109.181"
app.listen("8080", hostnamer, function () {
    console.log('服务开启');

    clicent.connect(options, function () {     // 监听后端端口号与后端连接
        console.log('连接其他后的后端');
        clicent.write("Web");
        // timer();
    });

    clicent.on('data', buffer => {   // net 接受后端传过来的数据  读取事件
        console.log('后端返回数据包', buffer);
        if (overageBuffer) {
            buffer = Buffer.concat([overageBuffer, buffer])
            console.log('接收buffer字节长度的', buffer.length)
        }
        overageBuffer = buffer;
        console.log('-------------111111接受一个完成的数据流包', overageBuffer);
        // console.log('00000000000000000000111111111111111111overageBuffern 数据量过大清空')
    });



    ////模拟请求数据
    app.get('/ifno',cors(),function(req,res){
        console.log(req.query.data);
         var info = req.query.data;
          if(info){
              axios.get('http://10.1.109.16:8080/v1/prod/main?line=line2&recipe=VR2-1-24').then((res)=>{
                  put = res.data;
                  console.log('---------------------',put);
                })
                res.send({code:200,data:put})
           }
    })




    app.get('/statistical', function (req, res) {
        var data = req.query.data;
        var statistical = JSON.parse(data);
        console.log(statistical);
        clicent.write(packet(statistical, 1009));
        let staticleng = 0;
        setTimeout(() => {
            console.log('ooooooooooooooooooooooo', overageBuffer)
            // console.log('返回整个包体', overageBuffer);
            while (staticleng = getPackageLength(overageBuffer)) {
                let staticpack = overageBuffer.slice(0, staticleng);
                console.log('============---1009获取整包--------------', staticpack);
                buffer = overageBuffer.slice(staticleng);
                overageBuffer = buffer;
                console.log('拆包后的剩余变量-------------', overageBuffer);
                const resul = subpackage(staticpack);
                // console.log(reult);
                if (resul.typer == 1009) {
                    if (resul.date == Streamid) {
                        let statisti = resul.contentr;
                        // console.log('------------', statisti.toString())
                        console.log('1009数据体', statisti.length);
                        res.send({ code: 200, data: JSON.parse(statisti) })
                    } else {
                        res.send({ code: 204, message: "数据返回为空,请重新请求" })
                    }
                }
            };
        }, 1200)
    })


    app.get("/get", function (req, res) {             //缺陷统计接口1000
        var data = req.query.data;
        var tempdata = JSON.parse(data);
        console.log(tempdata)
        clicent.write(packet(tempdata, 1000));
        let codelist = 0;
        setTimeout(() => {
            while (codelist = getPackageLength(overageBuffer)) {
                console.log('--------------------', overageBuffer)
                let datacode = overageBuffer.slice(0, codelist);
                buffer = overageBuffer.slice(codelist);
                overageBuffer = buffer;
                console.log('拆包后的剩余变量-------------', overageBuffer);
                const errdata = subpackage(datacode);
                if (errdata.typer == 1000) {
                    if (errdata.date == Streamid) {
                        let erritem = errdata.contentr;
                        res.send({ code: 200, data: JSON.parse(erritem) })
                    } else {
                        res.send({ code: 204, message: "数据返回为空,请重新请求" })
                    }
                }
            }
        }, 1200)
    });


    app.post("/Detection", mutipartMiddeware, cors(), function (req, res) {           // 检测数据接口1001 上传接口
        // console.log(req.query.data)  
        console.log(req.files);
        var fileobj = req.files;
        var data = req.query.data;
        var tempdataa = JSON.parse(data);
        let detecionlen = 0;
        console.log('--------------------', tempdataa);
        if (JSON.stringify(fileobj) == "{}") {
            console.log('内容为空');
            clicent.write(packet(tempdataa, 1001));
            setTimeout(() => {
                while (detecionlen = getPackageLength(overageBuffer)) {
                    let detetionpack = overageBuffer.slice(0, detecionlen);
                    buffer = overageBuffer.slice(detecionlen);
                    overageBuffer = buffer;
                    const detetionlist = subpackage(detetionpack);
                    if (detetionlist.typer == 1001) {
                        if (detetionlist.date == Streamid) {
                            let detetiondata = detetionlist.contentr;
                            let detecontlist = JSON.parse(detetiondata);
                            let tablelist = [];
                            let productlist = [];
                            for (let i = 0; i < detecontlist.table.length; i++) {
                                tablelist = detecontlist.table.slice(0, 50);
                                productlist = detecontlist.table;
                            }
                            setItem('productlist', JSON.stringify(productlist));
                            console.log('1222222222221212----截取列表后边条数', productlist.length)
                            detecontlist.table = tablelist;
                            res.send({ code: 200, data: detecontlist });
                            // res.send({ code: 200, data: JSON.parse(detetiondata) });
                        } else {
                            res.send({ code: 204, message: "数据返回为空,请重新请求" })
                        }
                    }
                }
            }, 2000)
        } else {
            console.log('有内容');
            var urlitems = [];
            var files = req.files.file;
            if (files.length > 1) {
                console.log('是数组');
                for (let i = 0; i < files.length; i++) {
                    urlitems.push(files[i].path.slice(3));
                }
                tempdataa.fileList = urlitems;
                // console.log(tempdataa);
                clicent.write(packet(tempdataa, 1001));
                setTimeout(() => {
                    while (detecionlen = getPackageLength(overageBuffer)) {
                        let detetionpack = overageBuffer.slice(0, detecionlen);
                        buffer = overageBuffer.slice(detecionlen);
                        overageBuffer = buffer;
                        const detetionlist = subpackage(detetionpack);
                        if (detetionlist.typer == 1001) {
                            if (detetionlist.date == Streamid) {
                                let detetiondata = detetionlist.contentr;
                                res.send({ code: 200, message: "文件上传成功", data: JSON.parse(detetiondata) });
                            } else {
                                res.send({ code: 204, message: "数据返回为空,请重新请求" })
                            }
                        }
                    }
                }, 2000)
            } else {
                console.log('不是数组');
                urlitems.push(files.path.slice(3));
                tempdataa.fileList = urlitems;
                console.log(tempdataa);
                clicent.write(packet(tempdataa, 1001));
                setTimeout(() => {
                    while (detecionlen = getPackageLength(overageBuffer)) {
                        let detetionpack = overageBuffer.slice(0, detecionlen);
                        buffer = overageBuffer.slice(detecionlen);
                        overageBuffer = buffer;
                        const detetionlist = subpackage(detetionpack);
                        if (detetionlist.typer == 1001) {
                            if (detetionlist.date == Streamid) {
                                let detetiondata = detetionlist.contentr;
                                let detecontlist = JSON.parse(detetiondata);
                                let tablelist = [];
                                let productlist = [];
                                for (let i = 0; i < detecontlist.table.length; i++){
                                    tablelist = detecontlist.table.slice(0, 50);
                                    productlist = detecontlist.table;
                                }
                                setItem('productlist', JSON.stringify(productlist));
                                console.log('1222222222221212----截取列表后边条数', productlist.length)
                                detecontlist.table = tablelist;
                                res.send({ code: 200, data: detecontlist });
                            } else {
                                res.send({ code: 204, message: "数据返回为空,请重新请求" })
                            }
                        }
                    }
                }, 2000)
            }
        }
    })



    ///产品履历查询 分页接口
    app.get('/productPage', cors(), function (req, res) {
        // console.log(req.query)
        let { pageSize, currentPage } = req.query;
        pageSize = pageSize ? pageSize : 50;
        currentPage = currentPage ? currentPage : 1;
        getItem('productlist').then(data => {
            // console.log('取出redis对象==================', JSON.parse(data));
            let datalist = JSON.parse(data);
            console.log(datalist.length);
            let total = datalist.length;                           //数据总条数
            let linmt = Math.ceil(datalist.length / pageSize);      //获取总页数
            let stat = currentPage * pageSize;                 //当前查询页面条数
            let srot = (currentPage - 1) * pageSize;
            // console.log('获取当前查询的页面条数--------------',stat)                 //当夜查询页面数
            // console.log('.......................',srot)
            // console.log('获取开始查询的页面',currentPage)
            // console.log('数据总页数',total);
            // console.log('页面总页数',linmt);
            //  let list = ZRANGEBYLEX (productlist) - + (LIMIT,stat,pageSize);
            //  console.log(list)
            if (currentPage) {
                console.log('当前请求页数', currentPage);
                for (let i = 0; i < datalist.length; i++) {
                    tabalelist = datalist.slice(srot, stat);
                }
                res.send({ code: 200, total: total, data: tabalelist, linmt: linmt })
            }
        }).catch(e => {
            console.log(e)
        });
    })


    /// 缺陷数据 分页接口  
    app.get('/drawbackpage', cors(), function (req, res) {
        let { pageSize, currentPage } = req.query;
        pageSize = pageSize ? pageSize : 50;
        currentPage = currentPage ? currentPage : 1;
        getItem('Defeclist').then(data => {
            console.log('取出redis对象==================', JSON.parse(data));
            let datalist = JSON.parse(data);
            console.log(datalist.length);
            let total = datalist.length;                           //数据总条数
            let linmt = Math.ceil(datalist.length / pageSize);      //获取总页数
            let stat = currentPage * pageSize;                 //当前查询页面条数
            let srot = (currentPage - 1) * pageSize;
            if (currentPage) {
                console.log('当前请求页数', currentPage);
                for (let i = 0; i < datalist.length; i++) {
                    tabalelist = datalist.slice(srot, stat);
                }
                res.send({ code: 200, total: total, data: tabalelist, linmt: linmt })
            }
        }).catch(e => {
            console.log(e)
        });
    })

    app.get('/dataanalysis', cors(), function (req, res) {        // 产品分分析页面数据 1002
        var data = req.query.data;
        var alysdata = JSON.parse(data);
        let dlyslen = 0;
        console.log('产品分析页面数据', alysdata);
        clicent.write(packet(alysdata, 1002));
        setTimeout(() => {
            while (dlyslen = getPackageLength(overageBuffer)) {
                let dlysdata = overageBuffer.slice(0, dlyslen);
                buffer = overageBuffer.slice(dlyslen);
                overageBuffer = buffer;
                const dlycont = subpackage(dlysdata);
                if (dlycont.typer == 1002) {
                    if (dlycont.date == Streamid) {
                        let dlycontitem = dlycont.contentr;
                        res.send({ code: 200, data: JSON.parse(dlycontitem) })
                    } else {
                        res.send({ code: 204, message: "数据返回为空,请重新请求" })
                    }
                }
            }
        }, 1000)
    })



    app.post('/Defectdensity', mutipartMiddeware, cors(), function (req, res) {        //缺陷密集性分析
        console.log('-------------------', req.body, req.files);
        var fileobj = req.files;
        var data = req.query.data;
        var densty = JSON.parse(data);
        var defedenlten = 0;
        console.log('--------------------', densty);
        if (JSON.stringify(fileobj) == "{}") {
            console.log('内容为空');
            clicent.write(packet(densty, 1003));
            setTimeout(() => {
                console.log('------------接口内部获取的数据流', overageBuffer);
                while (defedenlten = getPackageLength(overageBuffer)) {
                    let detedentionpack = overageBuffer.slice(0, defedenlten);
                    buffer = overageBuffer.slice(defedenlten);
                    overageBuffer = buffer;
                    const detedendata = subpackage(detedentionpack);
                    if (detedendata.typer == 1003) {
                        if (detedendata.date == Streamid) {
                            let detedata = detedendata.contentr;
                            let detelist = JSON.parse(detedata);
                            let tablelist = [];
                            let Defectdata = [];
                            for (let i = 0; i < detelist.table.length; i++) {
                                tablelist = detelist.table.slice(0, 50);
                                Defectdata = detelist.table;
                            }
                            setItem('Defeclist', JSON.stringify(Defectdata));
                            detelist.table = tablelist;
                            res.send({ code: 200, data: detelist });
                        } else {
                            res.send({ code: 204, message: "数据返回为空,请重新请求" })
                        }
                    }
                }
            }, 3000)
            return;
        } else {
            console.log('有内容');
            var urlitems = [];
            var files = req.files.file;
            if (files.length > 1) {
                console.log('是数组');
                for (let i = 0; i < files.length; i++) {
                    urlitems.push(files[i].path.slice(3));
                }
                densty.fileList = urlitems;
                // console.log(densty);
                clicent.write(packet(densty, 1003));
                setTimeout(() => {
                    while (defedenlten = getPackageLength(overageBuffer)) {
                        let detedentionpack = overageBuffer.slice(0, defedenlten);
                        buffer = overageBuffer.slice(defedenlten);
                        overageBuffer = buffer;
                        const detedendata = subpackage(detedentionpack);
                        if (detedendata.typer == 1003) {
                            if (detedendata.date == Streamid) {
                                let detedata = detedendata.contentr;
                                res.send({ code: 200, data: JSON.parse(detedata) });
                            } else {
                                res.send({ code: 204, message: "数据返回为空,请重新请求" })
                            }
                        }
                    }
                }, 3000)
            } else {
                console.log('不是数组');
                urlitems.push(files.path.slice(3));
                densty.fileList = urlitems;
                // console.log(densty);
                clicent.write(packet(densty, 1003));
                console.log('------------接口内部获取的数据流', overageBuffer);
                setTimeout(() => {
                    console.log('------------接口内部获取的数据流', overageBuffer);
                    while (defedenlten = getPackageLength(overageBuffer)) {
                        let detedentionpack = overageBuffer.slice(0, defedenlten);
                        buffer = overageBuffer.slice(defedenlten);
                        overageBuffer = buffer;
                        const detedendata = subpackage(detedentionpack);
                        if (detedendata.typer == 1003) {
                            if (detedendata.date == Streamid) {
                                let detedata = detedendata.contentr;
                                res.send({ code: 200, data: JSON.parse(detedata) });
                            } else {
                                res.send({ code: 204, message: "数据返回为空,请重新请求" })
                            }
                        }
                    }
                }, 3000)
            }
        }
    })



    app.post('/pmentdensity', mutipartMiddeware, cors(), function (req, res) {        ///设备密集型分析
        var fileobj = req.files;
        var data = req.query.data;
        var pmendata = JSON.parse(data);
        let pmenlen = 0;
        // console.log('--------------------', pmendata);Devtable
        if (JSON.stringify(fileobj) == "{}") {
            console.log('内容为空');
            clicent.write(packet(pmendata, 1004));
            setTimeout(() => {
                while (pmenlen = getPackageLength(overageBuffer)) {
                    let penpack = overageBuffer.slice(0, pmenlen);
                    buffer = overageBuffer.slice(pmenlen);
                    overageBuffer = buffer;
                    let pmenlist = subpackage(penpack);
                    if (pmenlist.typer == 1004) {
                        if (pmenlist.date == Streamid) {
                            let detetiondata = pmenlist.contentr;
                            let pmencont = JSON.parse(detetiondata);
                            let pmentable = [];
                            let Devtablelist = [];
                            for (let i = 0; i < pmencont.Devtable.length; i++) {
                                pmentable = pmencont.Devtable.slice(0, 50);
                                Devtablelist = pmencont.Devtable;
                            }
                            setItem('pmentd', JSON.stringify(Devtablelist));
                            pmencont.Devtable = pmentable;
                            res.send({ code: 200, data: pmencont });
                        } else {
                            res.send({ code: 204, message: "数据返回为空,请重新请求" })
                        }
                    }
                }
            }, 1500)
            return;
        } else {
            console.log('有内容');
            var urlitems = [];
            var files = req.files.file;
            if (files.length > 1) {
                console.log('是数组');
                for (let i = 0; i < files.length; i++) {
                    urlitems.push(files[i].path.slice(3));
                }
                pmendata.fileList = urlitems;
                // console.log(pmendata);
                clicent.write(packet(pmendata, 1004));
                setTimeout(() => {
                    while (pmenlen = getPackageLength(overageBuffer)) {
                        let penpack = overageBuffer.slice(0, pmenlen);
                        buffer = overageBuffer.slice(pmenlen);
                        overageBuffer = buffer;
                        let pmenlist = subpackage(penpack);
                        if (pmenlist.typer == 1004) {
                            if (pmenlist.date == Streamid) {
                                let detetiondata = pmenlist.contentr;
                                res.send({ code: 200, data: JSON.parse(detetiondata) });
                            } else {
                                res.send({ code: 204, message: "数据返回为空,请重新请求" })
                            }
                        }
                    }
                }, 1500)
            } else {
                console.log('不是数组');
                urlitems.push(files.path.slice(3));
                pmendata.fileList = urlitems;
                // console.log(pmendata);
                clicent.write(packet(pmendata, 1004));
                setTimeout(() => {
                    console.log('------------接口内部获取的数据流', overageBuffer);
                    while (pmenlen = getPackageLength(overageBuffer)) {
                        let penpack = overageBuffer.slice(0, pmenlen);
                        buffer = overageBuffer.slice(pmenlen);
                        overageBuffer = buffer;
                        let pmenlist = subpackage(penpack);
                        if (pmenlist.typer == 1004) {
                            if (pmenlist.date == Streamid) {
                                let detetiondata = pmenlist.contentr;
                                res.send({ code: 200, data: JSON.parse(detetiondata) });
                            } else {
                                res.send({ code: 204, message: "数据返回为空,请重新请求" })
                            }
                        }
                    }
                }, 1500)
            }
        }
    })

    ///设备密集型 分页接口
    app.get('/pmentd', cors(), function (req, res) {
        let { pageSize, currentPage } = req.query;
        pageSize = pageSize ? pageSize : 50;
        currentPage = currentPage ? currentPage : 1;
        getItem('pmentd').then(data => {
            console.log('取出redis对象==================', JSON.parse(data));
            let datalist = JSON.parse(data);
            console.log(datalist.length);
            let total = datalist.length;                           //数据总条数
            let linmt = Math.ceil(datalist.length / pageSize);      //获取总页数
            let stat = currentPage * pageSize;                 //当前查询页面条数
            let srot = (currentPage - 1) * pageSize;
            // console.log('获取当前查询的页面条数--------------',stat)                 //当夜查询页面数
            // console.log('.......................',srot)
            // console.log('获取开始查询的页面',currentPage)
            // console.log('数据总页数',total);
            // console.log('页面总页数',linmt);
            if (currentPage) {
                console.log('当前请求页数', currentPage);
                for (let i = 0; i < datalist.length; i++) {
                    tabalelist = datalist.slice(srot, stat);
                }
                res.send({ code: 200, total: total, data: tabalelist, linmt: linmt })
            }
        }).catch(e => {
            console.log(e)
        });
    })



    app.post('/Staffcapacity', cors(), function (req, res) {           /////人员产能PersonTable
        var data = req.query.data;
        var fileobj = req.files;
        var staffdata = JSON.parse(data);
        let stafflen = 0;
        console.log('--------------------', staffdata);
        if (JSON.stringify(fileobj) == "{}") {
            console.log('内容为空');
            clicent.write(packet(staffdata, 1005));
            setTimeout(() => {
                while (stafflen = getPackageLength(overageBuffer)) {
                    let staffpack = overageBuffer.slice(0, stafflen);
                    buffer = overageBuffer.slice(stafflen);
                    overageBuffer = buffer;
                    let stafflist = subpackage(staffpack);
                    if (stafflist.typer == 1005) {
                        if (stafflist.date == Streamid) {
                            let staffcenten = stafflist.contentr;
                            let staffitems = JSON.parse(staffcenten);
                            let staffTable = [];
                            let staffresis = [];
                            for (let i = 0; i < staffitems.PersonTable.length; i++) {
                                staffTable = staffitems.PersonTable.slice(0, 50);
                                staffresis = staffitems.PersonTable
                            }
                            staffitems.PersonTable = staffTable;
                            setItem('Staff', JSON.stringify(staffresis))
                            res.send({ code: 200, data: staffitems });
                        } else {
                            res.send({ code: 204, message: "数据返回为空,请重新请求" })
                        }
                    } 
                }
            }, 1000)
            return;
        } else {
            console.log('有内容');
            var urlitems = [];
            var files = req.files.file;
            if (files.length > 1) {
                console.log('是数组');
                for (let i = 0; i < files.length; i++) {
                    urlitems.push(files[i].path.slice(3));
                }
                staffdata.fileList = urlitems;
                // console.log(staffdata);
                clicent.write(packet(staffdata, 1005));
                setTimeout(() => {
                    while (stafflen = getPackageLength(overageBuffer)) {
                        let staffpack = overageBuffer.slice(0, stafflen);
                        buffer = overageBuffer.slice(stafflen);
                        overageBuffer = buffer;
                        let stafflist = subpackage(staffpack);
                        if (stafflist.typer == 1005) {
                            if (stafflist.date == Streamid) {
                                let staffcenten = stafflist.contentr;
                                res.send({ code: 200, data: JSON.parse(staffcenten) });
                            } else {
                                res.send({ code: 204, message: "数据返回为空,请重新请求" })
                            }
                        }
                    }
                }, 1000)
            } else {
                console.log('不是数组');
                urlitems.push(files.path.slice(3));
                staffdata.fileList = urlitems;
                // console.log(staffdata);
                clicent.write(packet(staffdata, 1005));
                setTimeout(() => {
                    while (stafflen = getPackageLength(overageBuffer)) {
                        let staffpack = overageBuffer.slice(0, stafflen);
                        buffer = overageBuffer.slice(stafflen);
                        overageBuffer = buffer;
                        let stafflist = subpackage(staffpack);
                        if (stafflist.typer == 1005) {
                            if (stafflist.date == Streamid) {
                                let staffcenten = stafflist.contentr;
                                res.send({ code: 200, data: JSON.parse(staffcenten) });
                            } else {
                                res.send({ code: 204, message: "数据返回为空,请重新请求" })
                            }
                        }
                    }
                }, 1000)
            }
        }
    })

    //人员产能 分页接口
    app.get('/staffpage', cors(), function (req, res) {
        let { pageSize, currentPage } = req.query;
        pageSize = pageSize ? pageSize : 50;
        currentPage = currentPage ? currentPage : 1;
        getItem('Staff').then(data => {
            console.log('取出redis对象==================', JSON.parse(data));
            let datalist = JSON.parse(data);
            console.log(datalist.length);
            let total = datalist.length;                           //数据总条数
            let linmt = Math.ceil(datalist.length / pageSize);      //获取总页数
            let stat = currentPage * pageSize;                 //当前查询页面条数
            let srot = (currentPage - 1) * pageSize;
            // console.log('获取当前查询的页面条数--------------',stat)                 //当夜查询页面数
            // console.log('.......................',srot)
            // console.log('获取开始查询的页面',currentPage)
            // console.log('数据总页数',total);
            // console.log('页面总页数',linmt);
            if (currentPage) {
                console.log('当前请求页数', currentPage);
                for (let i = 0; i < datalist.length; i++) {
                    tabalelist = datalist.slice(srot, stat);
                }
                res.send({ code: 200, total: total, data: tabalelist, linmt: linmt })
            }
        }).catch(e => {
            console.log(e)
        });
    })


    app.get('/capacitystacs', cors(), function (req, res) {           ///复判人员产能统计
        var data = req.query.data;
        var capacity = JSON.parse(data);
        console.log('复判人员产能统计', capacity);
        let comparalen = 0;
        clicent.write(packet(capacity, 1006));
        setTimeout(() => {
            while (comparalen = getPackageLength(overageBuffer)) {
                let comparadata = overageBuffer.slice(0, comparalen);
                buffer = overageBuffer.slice(comparalen);
                overageBuffer = buffer;
                let comparalist = subpackage(comparadata);
                if (comparalist.typer == 1006) {
                    if (comparalist.date == Streamid) {
                        let comparacent = comparalist.contentr;
                        res.send({ code: 200, data: JSON.parse(comparacent) });
                    } else {
                        res.send({ code: 204, message: "数据返回为空,请重新请求" })
                    }
                }
            }
        },900);
    })


    app.get('/Comparative', cors(), function (req, res) {                   ///缺陷对比分析
        var data = req.query.data;
        var parative = JSON.parse(data);
        console.log('缺陷对比分析', parative);
        let comtivelen = 0;
        clicent.write(packet(parative, 1007));
        setTimeout(() => {
            // console.log('///////////////////////////////',overageBuffer)
            while (comtivelen = getPackageLength(overageBuffer)) {
                let comtivepack = overageBuffer.slice(0, comtivelen);
                buffer = overageBuffer.slice(comtivelen);
                overageBuffer = buffer;
                let comtviedata = subpackage(comtivepack);
                if (comtviedata.typer == 1007) {
                    if (comtviedata.date == Streamid) {
                        let comdatalist = comtviedata.contentr;
                        res.send({ code: 200, data: JSON.parse(comdatalist) });
                    } else {
                        res.send({ code: 204, message: "数据返回为空,请重新请求" })
                    }
                }
            }
        }, 900);
    })

    app.get('/Capacity', cors(), function (req, res) {
        var data = req.query.data;
        var Capacity = JSON.parse(data);
        console.log('产能对比分析', Capacity);
        let capcitylen = 0;
        clicent.write(packet(Capacity, 1008));
        setTimeout(() => {
            while (capcitylen = getPackageLength(overageBuffer)) {
                let captypack = overageBuffer.slice(0, capcitylen);
                buffer = overageBuffer.slice(capcitylen);
                overageBuffer = buffer;
                let capadata = subpackage(captypack);
                if (capadata.typer == 1008) {
                    if (capadata.date == Streamid) {
                        let capacent = capadata.contentr
                        res.send({ code: 200, data: JSON.parse(capacent) })
                    } else {
                        res.send({ code: 204, message: "数据返回为空,请重新请求" })
                    }
                }
            }
        }, 1700);
    })

    app.get('/warning', cors(), function (req, res) {
        var data = req.query.data;
        var warning = JSON.parse(data);
        console.log('连续预警', warning);
        clicent.write(packet(warning, 1010));
        let warningleng = 0;
        setTimeout(() => {
            console.log('---------------1010数据', overageBuffer);
            while (warningleng = getPackageLength(overageBuffer)) {
                let warapack = overageBuffer.slice(0, warningleng);
                buffer = overageBuffer.slice(warningleng);
                overageBuffer = buffer;
                let warndata = subpackage(warapack);
                //  console.log(warndata)
                if (warndata.typer == 1010) {
                    //    console.log('1111111111111111')
                    if (warndata.date == Streamid) {
                        // console.log('22222222222222222222')
                        let waracont = warndata.contentr;
                        res.send({ code: 200, data: JSON.parse(waracont) });
                    }
                }
            }
        }, 2000)
    })

    app.get('/airplane', cors(), function (req, res) {
        var data = req.query.data;
        var airlan = JSON.parse(data);
        console.log('1011---------', airlan);
        clicent.write(packet(airlan, 1011));
        let airpaneleng = 0;
        setTimeout(() => {
            if (overageBuffer > overageBuffer) {
                console.log('-------------------overageBuffern 数据量过大')
            }
            while (airpaneleng = getPackageLength(overageBuffer)) {
                let aranpack = overageBuffer.slice(0, airpaneleng);
                console.log('---------------1011数据11111111111111111', aranpack);
                buffer = overageBuffer.slice(airpaneleng);
                overageBuffer = buffer;
                let airandata = subpackage(aranpack);
                if (airandata.typer == 1011) {
                    if (airandata.date == Streamid) {
                        let arancont = airandata.contentr;
                        res.send({ code: 200, data: JSON.parse(arancont) })
                    }
                }
            }
        }, 1000)
    })


    app.get('/imgwargin', cors(), function (req, res) {
        var data = req.query.data;
        var imgwargin = JSON.parse(data);
        console.log('1012-------------', imgwargin);
        let imgwarlen = 0;
        clicent.write(packet(imgwargin, 1012));
        setTimeout(() => {
            console.log('---------------1012数据', overageBuffer);
            while (imgwarlen = getPackageLength(overageBuffer)) {
                let imgwarginpack = overageBuffer.slice(0, imgwarlen);
                buffer = overageBuffer.slice(imgwarlen);
                overageBuffer = buffer;
                let imgwardata = subpackage(imgwarginpack);
                if (imgwardata.typer == 1012) {
                    if (imgwardata.date == Streamid) {
                        let imgwarcont = imgwardata.contentr;
                        res.send({ code: 200, data: JSON.parse(imgwarcont) })
                    }
                }
            }
        }, 3500)
    })

    app.get('/Warningstatements', cors(), function (req, res) {
        var a = req.query.data;
        //  var ai = JSON.parse(a)
        clicent.write(packet(a, 1014));
        let statementslen = 0;
        setTimeout(() => {
            console.log('---------------1014数据', overageBuffer);
            while (statementslen = getPackageLength(overageBuffer)) {
                let statemenpck = overageBuffer.slice(0, statementslen);
                buffer = overageBuffer.slice(statementslen);
                overageBuffer = buffer;
                let statedata = subpackage(statemenpck);
                if (statedata.typer == 1014) {
                    if (statedata.date == Streamid) {
                        let statemencont = statedata.contentr;
                        console.log('111111111111111111数据列表回来')
                        res.send({ code: 200, data: JSON.parse(statemencont) });
                    }
                }
            }
        }, 1500)
    })


    app.get('/listinformation', cors(), function (req, res) {
        var a = req.query.data;
        // console.log('1111111111111111111111', a)
        clicent.write(packet(a, 1013));
        let listinfolen = 0;
        setTimeout(() => {
            console.log('---------------1013数据', overageBuffer);
            while (listinfolen = getPackageLength(overageBuffer)) {
                let listinfopack = overageBuffer.slice(0, listinfolen);
                buffer = overageBuffer.slice(listinfolen);
                overageBuffer = buffer;
                let listinfodata = subpackage(listinfopack);
                if (listinfodata.typer == 1013) {
                    if (listinfodata.date == Streamid) {
                        let listinocont = listinfodata.contentr;
                        res.send({ code: 200, data: JSON.parse(listinocont) })
                    }
                }
            }
        }, 1000)
    })

    //实时看板接口 折线图 接口
    app.get('/Real-time-lineList', cors(), function (req, res) {
        var data = req.query.data;
        var realdata = JSON.parse(data);
        console.log('-----------', realdata)
        clicent.write(packet(realdata, 1015));
        let reailen = 0;
        setTimeout(() => {
            console.log('---------------1015数据', overageBuffer);
            while (reailen = getPackageLength(overageBuffer)) {
                let realpack = overageBuffer.slice(0, reailen);
                buffer = overageBuffer.slice(reailen);
                overageBuffer = buffer;
                let realtimedata = subpackage(realpack);
                if (realtimedata.typer == 1015) {
                    if (realtimedata.date == Streamid) {
                        let realcent = realtimedata.contentr;
                        res.send({ code: 200, data: JSON.parse(realcent) })
                    }
                }
            }
        }, 1000)
    })
});

clicent.on('error', function (e) {    // net 连接错误提示
    console.error('服务端连接失败' + e)
})

clicent.on('end', function () {  //  net 用户发送关闭数据包 //如果某个客户端断开连接，node控制台就会打印出来
    console.log("end event");

})





































































